package com.globallogic.vkulyk.SSHTool;

import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

public class XMLHandler extends DefaultHandler {

	private String targetName = "";
	private ArrayList<SocketDataKeeper> sockets = new ArrayList<SocketDataKeeper>();
	private SocketDataKeeper socket = null;;
	private ArrayList<ActionDataKeeper> actions = new ArrayList<ActionDataKeeper>();
	private ActionDataKeeper action = null;
	private boolean elementIsTheTarget = false;
	private boolean parsingIsFinished = false;
	private static final Logger logger = LoggerFactory
			.getLogger(XMLHandler.class);

	public XMLHandler(String fileArg, String targetNameArg) {
		targetName = targetNameArg;
		try {
			SAXParserFactory spfac = SAXParserFactory.newInstance();
			SAXParser sp = spfac.newSAXParser();
			sp.parse(fileArg, this);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}

	public void startElement(String uri, String localName, String qName,
			Attributes attributes) {
		try {
			if (qName.equalsIgnoreCase("target")
					&& attributes.getValue("name").equals(targetName)) {
				elementIsTheTarget = true;
			} else if (elementIsTheTarget && qName.equalsIgnoreCase("socket")) {
				socket = new SocketDataKeeper(attributes.getValue("host"),
						Integer.parseInt(attributes.getValue("port")));
				sockets.add(socket);
			} else if (elementIsTheTarget && qName.equalsIgnoreCase("action")) {
				if (attributes.getValue("type").equals("execute_command")) {
					action = new CommandExecutionDataKeeper(
							attributes.getValue("type"),
							attributes.getValue("user"),
							attributes.getValue("password"),
							attributes.getValue("command"));
				} else if (attributes.getValue("type").equals("execute_script")) {
					action = new ScriptExecutionDataKeeper(
							attributes.getValue("type"),
							attributes.getValue("user"),
							attributes.getValue("password"),
							attributes.getValue("local_file"));
				} else if (attributes.getValue("type").equals("put")
						|| attributes.getValue("type").equals("get")) {
					action = new FileCopyDataKeeper(
							attributes.getValue("type"),
							attributes.getValue("user"),
							attributes.getValue("password"),
							attributes.getValue("src_file"),
							attributes.getValue("dst_dir"));
				}
				actions.add(action);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}

	public void endElement(String uri, String localName, String qName) {
		try {
			if (qName.equalsIgnoreCase("target")) {
				elementIsTheTarget = false;
				parsingIsFinished = true;
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}

	public void endDocument() {
		parsingIsFinished = true;
	}

	public SocketsAndActionsDataKeeper parseXML() {
		while (parsingIsFinished == false) {
		}
		return new SocketsAndActionsDataKeeper(sockets, actions);
	}

}
